package com.example.shushiyunsst.fragment.main.home.child.fu_li_pool.sub

import android.annotation.SuppressLint
import android.os.Bundle
import android.view.View
import android.widget.TextView
import androidx.appcompat.widget.Toolbar
import com.alibaba.fastjson.JSON
import com.example.library_common.adapter.JssBaseViewHolder
import com.example.library_common.album.utils.Utils
import com.example.library_common.base.SimpleListFragment
import com.example.library_common.bean.CommonBean
import com.example.library_common.networking.RestClient
import com.example.shushiyunsst.R
import com.example.shushiyunsst.fragment.main.home.child.fu_li_pool.bean.DangQianFuLiBean
import com.example.wallet.db.UserInfoManager
import ikidou.reflect.TypeBuilder
import java.lang.reflect.Type

/**
 * 当前福利
 * */
class DangQianFuLiFragment : SimpleListFragment<DangQianFuLiBean>() {

    companion object {
        fun newInstance(): DangQianFuLiFragment {
            val args = Bundle()
            val fragment = DangQianFuLiFragment()
            fragment.arguments = args
            return fragment
        }
    }

    private lateinit var allValueTv:TextView

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        isAddDividerItemDecoration = false
        isEnableLoadMore = false
    }

    @SuppressLint("SetTextI18n")
    override fun initView(view: View) {
        super.initView(view)
        view.setBackgroundResource(R.color.bg_color)

        mHeader.removeAllViews()
        addHeader(R.layout.dang_qian_fu_li_header_fragment)
        val mToolbar:Toolbar= view.findViewById (R.id.mToolbar)
        mToolbar.setNavigationOnClickListener { onBackPressedSupport() }
      
        mSwipeRefreshLayout.setColorScheme(R.color.nav_bar_bg_color)
        mRecyclerView.overScrollMode = View.OVER_SCROLL_NEVER
        mSwipeRefreshLayout.setProgressBackgroundColor(R.color.colorAccent)

        val inflate = View.inflate(_mActivity, R.layout.dang_qian_fu_li_list_header_fragment, null)

        mAdapter.setHeaderView(inflate)

          allValueTv = inflate.findViewById(R.id.allValueTv)

//        allValueTv.text="1563.0000"
    }

    override fun getItemLayout() = R.layout.dang_qian_fu_li_fragment

    override fun convertItem(helper: JssBaseViewHolder?, item: DangQianFuLiBean?) {
        helper?.setText(R.id.seasonTv, "${item?.localTimes}期")
            ?.setText(R.id.typeTv,item?.typeDesc)
            ?.setText(R.id.valueTv,"${item?.totalNums}")
            ?.setText(R.id.timePoint,item?.timesDesc)
    }

    override fun getListType(): Type {
        return TypeBuilder.newInstance(CommonBean::class.java)
            .beginSubType(List::class.java)
            .addTypeParam(DangQianFuLiBean::class.java).endSubType().build()
    }

    override fun netRequest() {
//        val jsonFromRaw = Utils.getJsonFromRaw(_mActivity, R.raw.dang_qian_fu_li)
//        val jsonFromRaw =""
//        parseDate(jsonFromRaw)

        val userInfo = UserInfoManager.userInfoManager.getUserInfoWrapper()?.userInfo

        val map = mapOf(
            "uid" to "${userInfo?.uid}"
        )
        RestClient.builder()
            .raw(JSON.toJSONString(map))
            .url(R.string.queryLogWelfare)
            .success {
                parseDate(it)
            }
            .error { _, _ -> onFailed() }
            .failure { onFailed() }
            .build().post()
    }

    override fun parseDate(data: String?) {
//        super.parseDate(data)
        val obj = JSON.parseObject(data)
        val list = obj["list"] ?: ""
        val array = JSON.parseArray(JSON.toJSONString(list), DangQianFuLiBean::class.java)
        loadListDate(array)
       val totalNums=obj["totalNums"].toString()
        allValueTv.text=totalNums
    }
}